package com.itheima.text;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(urlPatterns ="/loginStudentServlet")
public class LoginStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String remember = req.getParameter("remember");


        if (remember != null) {
            //添加cookie
            Cookie cookie1 = new Cookie("remember",remember);
            Cookie cookie2 = new Cookie("password",password);
            Cookie cookie3 = new Cookie("username",username);


            cookie1.setMaxAge(3600);
            cookie2.setMaxAge(3600);
            cookie3.setMaxAge(3600);


            resp.addCookie(cookie1);
            resp.addCookie(cookie2);
            resp.addCookie(cookie3);
        }
//        if (username!=null||username!="") {
            HttpSession session = req.getSession();
            session.setAttribute("username",username);
            resp.sendRedirect("/day5/index.jsp");
        /*}else{
//            resp.setContentType("com/itheima/text/html;charset=utf-8");
            PrintWriter pw = resp.getWriter();
            pw.write("用户名为空，3秒钟跳转到登陆页面。。。");
            resp.setHeader("Refresh","3;URL=/day5/login.jsp");
        }*/


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
